package com.itheima;

import com.itheima.manager_system.domain.Person;
import com.itheima.manager_system.domain.Student;

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        ArrayList<Person> list = new ArrayList<>();
        Student st1 = new Student();
        Student st2 = new Student();
        Student st3 = new Student();
        st1.setId(003);
        st1.setName("aaa");
        st2.setId(001);
        st2.setName("bbb");
        st3.setId(002);
        st3.setName("ccc");
        list.add(st1);
        list.add(st2);
        list.add(st3);
//        for (Person p : list) {
//            System.out.println(p.getId()+"\t"+p.getName());
//        }
//        System.out.println("---------------");
        sortList(list);
        for (Person p : list) {
            System.out.println(p.getId()+"\t"+p.getName());
        }



    }

    public static void  sortList(ArrayList<Person> list) {
        ArrayList<Person> list2 = new ArrayList<>();
        ArrayList<Integer> id = new ArrayList<>();
        for (Person i : list) {
            id.add(i.getId());
        }
        Collections.sort(id);
        for (Integer i : id) {
            int index = getIndex(list, i);
          list2.add(list.get(index));
        }
        list.clear();
        for (Person p : list2) {
            list.add(p);
        }
    }


    public static int getIndex(ArrayList<Person> list, int id) {
            /*
            编号唯一性的判断方法
            遍历list,用id逐个匹配list中每个元素的id,如果匹配到就返回索引
            如果没有匹配到,最后返回-1
             */
        for (int i = 0; i < list.size(); i++) {
            if (id == ((list.get(i).getId()))) {
                return i;
            }
        }
        return -1;
    }
}
